package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.MarkStandard;
import com.check.pojo.PageObject;
import com.check.service.MarkStandardService;

@Controller
public class MarkStandardController extends BaseController {
	
	@Autowired
	@Qualifier("markStandardService")
	private MarkStandardService markStandardService;
	
	@RequestMapping("markStandardQuery.do")
	public ModelAndView markStandardQuery(HttpServletRequest request,HttpServletResponse response
			,@ModelAttribute("pageObject")PageObject pageObject){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<MarkStandard> markStandardList=markStandardService.findAllMarkStandard(pageObject);
		pageObject.setUrl("markStandardQuery.do");
		pageObject.setTotal(markStandardService.findMarkStandardCount());
		return new ModelAndView("page/markstandard/markStandardList")
					.addObject("path",request.getContextPath())
					.addObject("markStandardList",markStandardList)
					.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toAddMarkStandard.do")
	public ModelAndView toAddMarkStandard(HttpServletRequest request,HttpServletResponse response){
		
		return new ModelAndView("page/markstandard/markStandardAdd")
					.addObject("path",request.getContextPath());
	}
	
	@RequestMapping("findMarkStandard.do")
	public void findMarkStandard(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String standId=request.getParameter("standId");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			MarkStandard findMarkStandard = markStandardService.findByStandId(standId);
			if(findMarkStandard!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping(value="addMarkStandard.do",method = RequestMethod.POST)
	public void addMarkStandard(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markStandard")MarkStandard markStandard){
			PrintWriter out = null;
			String flag = "0";
			
			try {
				out = response.getWriter();
				markStandard.setStandName(URLDecoder.decode(request.getParameter("standName"),  "UTF-8"));
				if(markStandardService.insertMarkStandard(markStandard)){
					flag = "1";
				}
				saveSysLog(RE_MASTAND, LOG_ADD, request);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out != null){
					out.flush();
					out.close();
				}
			}
		}
	
		@RequestMapping("toUpdateMarkStandard.do")
		public ModelAndView toUpdateMarkStandard(HttpServletRequest request,HttpServletResponse response){
			String standId = request.getParameter("standId");
			MarkStandard markStandard = markStandardService.findByStandId(standId);
			return new ModelAndView("page/markstandard/markStandardUpdate")
						.addObject("path",request.getContextPath())
						.addObject("markStandard",markStandard);
		}
		
		@RequestMapping(value="updateMarkStandard.do",method = RequestMethod.POST)
		public void updateMarkStandard(HttpServletRequest request,HttpServletResponse response,
				@ModelAttribute("markStandard")MarkStandard markStandard){
				PrintWriter out = null;
				String flag = "0";
				
				try {
					out = response.getWriter();
					markStandard.setStandName(URLDecoder.decode(request.getParameter("standName"),  "UTF-8"));
					if(markStandardService.updateMarkStandard(markStandard)){
						flag = "1";
					}
					saveSysLog(RE_MASTAND, LOG_UPDATE, request);
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					out.println(flag);
					if(out != null){
						out.flush();
						out.close();
					}
				}
			}
		
		
		@RequestMapping(value="deleteMarkStandard.do",method = RequestMethod.POST)
		public void deleteMarkStandard(HttpServletRequest request,HttpServletResponse response){
				PrintWriter out = null;
				String flag = "0";
				
				try {
					out = response.getWriter();
					String standId=request.getParameter("standId");
					if(markStandardService.deleteMarkStandard(standId)){
						flag = "1";
					}
					saveSysLog(RE_MASTAND, LOG_DELETE, request);
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					out.println(flag);
					if(out != null){
						out.flush();
						out.close();
					}
				}
			}
}
